<?php
namespace Admin\Controller;

//活动管理
use Common\Controller\AdminbaseController;

class ActivityController extends AdminbaseController
{
    protected $activity_model;
    protected $banners_model;
    protected $merchant_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->activity_model = D("Common/Activity");
        $this->banners_model = M("activity_banners");
        $this->merchant_model = M("merchant");
    }

    //活动列表
    public function index()
    {
        /**搜索条件**/
        $activity_title = I('request.activity_title');
        $activity_brand_type = I('request.activity_brand_type');
        if($activity_title){
            $where['activity_title'] = array('like',"%$activity_title%");
        }
        if($activity_brand_type){
            $where['activity_brand_type'] = $activity_brand_type;
        }

        $count=$this->activity_model->where($where)->count();
        $page = $this->page($count, 10);
        $list = $this->activity_model
            ->where($where)
            ->order("sort desc,id desc")
            ->limit($page->firstRow, $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign('list',$list);
        $this->assign('activity_brand_type',$activity_brand_type);
        $this->display();
    }

    //活动新增
    public function add()
    {
        $merchant = $this->merchant_model->field('id,name')->select();
        $this->assign('merchant',$merchant);
        $this->display();
    }

    // 活动添加提交
    public function add_post(){
        if (IS_POST) {

            if ($this->activity_model->create() !== false) {
                $date = $_POST;
                $date['merchant_id'] = implode(',',$date['merchant_id']);
                /*当前管理员name*/
                $date['create_user'] = get_current_admin_name();
                $date['create_time'] = get_admin_time();
                $result = $this->activity_model->add($date);
                if ($result !== false) {
                    for ($i = 0;$i < count($date['photos_url']) ;$i++){
                        $banners['banner_img'] = $date['photos_url'][$i];
                        $banners['activity_id'] = $result;
                        $banners['use_flg'] = 1;
                        $banners['create_user'] = get_current_admin_name();
                        $banners['create_time'] = get_admin_time();
                        $this->banners_model->add($banners);
                    }
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->activity_model->getError());
            }
        }
    }

    //活动编辑
    public function edit()
    {
        $id = I('get.id',0,'intval');
        $activity = $this->activity_model->where(array('id' => $id))->find();
        $banners = $this->banners_model->where(array('activity_id' => $id))->select();
        $merchant = $this->merchant_model->field('id,name')->select();
        $activity['merchant_id'] = explode(',',$activity['merchant_id']);
        $activity['merchant_id_count'] = count($activity['merchant_id']);
        $this->assign('merchant',$merchant);
        $this->assign('activity',$activity);
        $this->assign('banners',$banners);
        $this->display();

    }

    //搜索商户
//    public function merchant_list(){
//        $name = I('post.name');
//        if($name){
//            $where['name'] = array('like',"%$name%");
//        }
//        $merchant = $this->merchant_model->field('id,name')->where($where)->select();
//        $this->ajaxReturn($merchant);
//    }

    //活动编辑提交
    public function edit_post()
    {
        if (IS_POST) {
            if ($this->activity_model->create() !== false) {
                $date = $_POST;
                $date['merchant_id'] = implode(',',$date['merchant_id']);
                /*当前管理员name*/
                $date['update_user'] = get_current_admin_name();
                $date['update_time'] = get_admin_time();
                $result = $this->activity_model->save($date);
                if ($result !== false) {
                    $this->banners_model->where(array('activity_id'=>$date['id']))->delete();
                    for ($i = 0;$i < count($date['photos_url']) ;$i++){
                        $banners['banner_img'] = $date['photos_url'][$i];
                        $banners['activity_id'] = $date['id'];
                        $banners['use_flg'] = 1;
                        $banners['create_user'] = get_current_admin_name();
                        $banners['create_time'] = get_admin_time();
                        $this->banners_model->add($banners);
                    }
                    $this->success("编辑成功！");
                } else {
                    $this->error("编辑失败！");
                }
            } else {
                $this->error($this->activity_model->getError());
            }
        }

    }

    // 活动删除
    public function delete(){
        $id = I('get.id',0,'intval');
        $activity = $this->activity_model->where(array('id' => $id))->find();
        $imgurl = $activity['activity_icon'];
        if ($this->activity_model->delete($id)!==false) {
            unlink('./'.C("UPLOADPATH").$imgurl);
            $banners = $this->banners_model->where(array('activity_id' => $id))->select();
            for ($i = 0;$i < count($banners);$i++){
                unlink('./'.C("UPLOADPATH").$banners[$i]['banner_img']);
            }
            $this->banners_model->where(array('activity_id'=>$id))->delete();
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }


}